import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/core/utils/customEndDrawer.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/get_feign_material_points_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/get_feign_page_detail_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/inventory_system/emergency_checklist/emergency_material_list/emergency_material_list_controller.dart';

///应急物资清单
class EmergencyMaterialListPage extends StatelessWidget {
  EmergencyMaterialListPage({super.key});

  final controller = Get.put(EmergencyMaterialListController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            backgroundColor: AppColors.bg_color_grey,
            appBar: CustomAppBar(
              titleText: '应急物资清单',
            ),
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(),
                      ),
                      AppGaps.hGap8,
                      _screen(),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<GetFeignPageDetailEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.feignPageDetail,
                    itemBuilder: (item, index) {
                      return _itemWidget(item);
                    },
                  ))
                ],
              ),
            ),
            endDrawer: CustomEndDrawer(
              centerContext: Column(
                children: [
                  _selectType(
                    '物资名称',
                    input: Container(
                        height: 38.w,
                        padding: EdgeInsets.symmetric(horizontal: 12.w),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border.all(color: const Color(0xffF0F0F0), width: 1.w),
                            borderRadius: BorderRadius.all(Radius.circular(8.w))),
                        child: Row(
                          children: [
                            Expanded(
                                child: Obx(() => TextField(
                                    keyboardType: TextInputType.text,
                                    textInputAction: TextInputAction.done,
                                    controller:
                                        TextEditingController(text: controller.searchX.value.isNotEmpty ? controller.searchX.value : controller.searchSelect),
                                    onChanged: (value) {
                                      controller.searchSelect = value;
                                    },
                                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                                    decoration: InputDecoration(
                                      counterText: '',
                                      //防止适配不居中
                                      isCollapsed: true,
                                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                                      contentPadding: EdgeInsets.only(left: 8.w),
                                      hintText: '请输入搜索内容',
                                      hintStyle: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                                    ))))
                          ],
                        )),
                  ),
                  _selectType(
                    '负责人',
                    input: Container(
                        height: 38.w,
                        padding: EdgeInsets.symmetric(horizontal: 12.w),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border.all(color: const Color(0xffF0F0F0), width: 1.w),
                            borderRadius: BorderRadius.all(Radius.circular(8.w))),
                        child: Row(
                          children: [
                            Expanded(
                                child: TextField(
                                    keyboardType: TextInputType.text,
                                    textInputAction: TextInputAction.done,
                                    controller: TextEditingController(text: controller.searchResponsiblePerson),
                                    onChanged: (value) {
                                      controller.searchResponsiblePerson = value;
                                    },
                                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                                    decoration: InputDecoration(
                                      counterText: '',
                                      //防止适配不居中
                                      isCollapsed: true,
                                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                                      contentPadding: EdgeInsets.only(left: 8.w),
                                      hintText: '请输入搜索内容',
                                      hintStyle: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                                    )))
                          ],
                        )),
                  ),
                  _selectType(
                    '联系电话',
                    input: Container(
                        height: 38.w,
                        padding: EdgeInsets.symmetric(horizontal: 12.w),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border.all(color: const Color(0xffF0F0F0), width: 1.w),
                            borderRadius: BorderRadius.all(Radius.circular(8.w))),
                        child: Row(
                          children: [
                            Expanded(
                                child: TextField(
                                    keyboardType: TextInputType.text,
                                    textInputAction: TextInputAction.done,
                                    controller: TextEditingController(text: controller.searchContactNumber),
                                    onChanged: (value) {
                                      controller.searchContactNumber = value;
                                    },
                                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                                    decoration: InputDecoration(
                                      counterText: '',
                                      //防止适配不居中
                                      isCollapsed: true,
                                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                                      contentPadding: EdgeInsets.only(left: 8.w),
                                      hintText: '请输入搜索内容',
                                      hintStyle: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                                    )))
                          ],
                        )),
                  ),
                  _alignTitle('物资点'),
                  SingleChildScrollView(
                      child: Column(
                    children: controller.getFeignMaterialPointsListEntityList.map(_questW).toList(),
                  )),
                ],
              ),
              reset: controller.reset,
              confirm: controller.confirm,
            ),
          );
        });
  }

  ///搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    controller: TextEditingController(text: controller.searchX.value),
                    onChanged: (value) {
                      controller.searchX.value = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入物资名称',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  Widget _selectType(String firstName, {Widget? input}) {
    return Column(
      children: [
        _alignTitle(firstName),
        AppGaps.vGap16,
        input ??
            Container(
              height: 38.w,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
                border: Border.all(width: 1.w, color: const Color(0xFFE3E7EE)),
              ),
              child: Row(
                children: [
                  AppGaps.hGap20,
                  Expanded(
                    child: Text(
                      '',
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: const Color(0xFFA0A7B5),
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                  SvgPicture.asset(AppImages.icon_filter_right_arrow),
                  AppGaps.hGap20,
                ],
              ),
            ).clickOnce(() {
              if (firstName == '') {
              } else if (firstName == '落实情况') {}
            }),
        AppGaps.vGap16,
      ],
    );
  }

  /// 标题
  Widget _title(String title, String? value) {
    return Row(
      children: [
        Text(
          title,
          style: TextStyle(
            color: const Color(0xffA5A6B5),
            fontSize: 13.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
        if (value != null)
          Expanded(
              child: Text(
            value,
            style: TextStyle(
              color: const Color(0xff495061),
              fontSize: 13.sp,
              fontWeight: FontWeight.w400,
            ),
          )),
      ],
    );
  }

  Widget _questW(GetFeignMaterialPointsListEntity item) {
    return Container(
      margin: EdgeInsets.only(top: 10.w, left: 20.w),
      child: Row(
        children: [
          _colorfulCheckBox(item),
          AppGaps.hGap10,
          Row(
            children: [
              Text(
                item.nameOfMaterialPoint ?? '无',
                style: TextStyle(
                  fontSize: 12.sp,
                  color: Colors.black,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _colorfulCheckBox(GetFeignMaterialPointsListEntity item) {
    return Theme(
      data: ThemeData(unselectedWidgetColor: const Color(0xFFDCDCDC)),
      child: SizedBox(
        width: 16.w,
        height: 16.w,
        child: Checkbox(
            // checkColor: const Color(0xFF3B62FF),
            overlayColor: MaterialStateProperty.all(Colors.blue),
            materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
            value: item.checkboxSelected,
            onChanged: (value) {
              item.checkboxSelected = !item.checkboxSelected;
              controller.update();
            }),
      ),
    );
  }

  Widget _itemWidget(GetFeignPageDetailEntity item) {
    return CustomTitleChartPage(
        title: item.materialName ?? '',
        child: Column(
          children: [
            Row(
              children: [
                Expanded(child: _title('物资名称:', item.materialName ?? '')),
                Expanded(child: _title('物资点名称:', item.materialPointIdName ?? '')),
              ],
            ),
            AppGaps.vGap10,
            Row(
              children: [
                Expanded(child: _title('物资数量:', '${item.quantity ?? ''}')),
                Expanded(child: _title('物资类型:', item.materialClassificationName ?? '')),
              ],
            ),
            AppGaps.vGap10,
            Row(
              children: [
                Expanded(child: _title('存放地点:', item.place ?? '')),
                Expanded(child: _title('负责人:', item.responsiblePerson ?? '')),
              ],
            ),
            AppGaps.vGap10,
            Row(
              children: [
                Expanded(child: _title('负责人电话:', item.contactNumber ?? '')),
                Expanded(child: _title('', '')),
              ],
            ),
          ],
        ));
  }

  Widget _alignTitle(String title) {
    return Align(
      alignment: Alignment.centerLeft,
      child: Text(
        title,
        style: TextStyle(
          fontSize: 14.sp,
          fontWeight: FontWeight.w400,
          color: const Color(0xFF262F38),
        ),
      ),
    );
  }
}
